﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_TOOL.Tool.Forms;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_TOOL.Tool.UserControls
{
    public partial class ucFrequency : DevExpress.XtraEditors.XtraUserControl
    {
        private int index = 0;
        public int _action = 1;
        FrequencyHelper helper = new FrequencyHelper(new Frequency());
        private LogUtils log;
        DXPopupMenu menu;
        #region Handle Methods

        public ucFrequency()
        {
            InitializeComponent();
            InitPopupMenu();
            BindDatatoGrid();
            gridView1.BestFitColumns();
            this.dropbtExport.DropDownControl = menu;
            log = new LogUtils();
        }

        private void btAddNew_Click_1(object sender, EventArgs e)
        {
            AddNew();
        }

        private void btEdit_Click_1(object sender, EventArgs e)
        {
            EditRow();
        }

        private void btDelete_Click_1(object sender, EventArgs e)
        {
            DeleteRows();
        }


        private void gridView1_CustomDrawEmptyForeground_1(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {
            if (gridView1.RowCount != 0)
                return;

            StringFormat drawFormat = new StringFormat();

            drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

            e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

        }

        private void gridView1_DoubleClick_1(object sender, EventArgs e)
        {
            var gridView = (GridView)sender;
            var point = gridView.GridControl.PointToClient(Control.MousePosition);

            DoDoubleClick(gridView, point);
        }


        private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddNew();
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditRow();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DeleteRows();
        }
        #endregion

        public void BindDatatoGrid()
        {
            FrequencyHelper helper = new FrequencyHelper(new Frequency());
            DataTable dt = helper.GetAll();

            gridControl1.DataSource = dt;
            Utilities.GridUtil.SelectRow(gridView1, index, _action);
        }

        private void DoDoubleClick(GridView v, Point p)
        {
            _action = 1;
            index = gridView1.FocusedRowHandle;
            var info = v.CalcHitInfo(p);
            if (info.InRow || info.InRowCell)
            {
                var row = new DataRow[gridView1.SelectedRowsCount];
                row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
                string frequencyId = row[0][ToolConstants.Entities.Frequency.FieldName.FrequencyID].ToString();
                helper.ValueObject.FrequencyID = frequencyId;
                helper.LoadOne();

                frm_FrequencyEdit fr = new frm_FrequencyEdit(this);
                fr.frequencyId = frequencyId;
                fr.frequencyName = helper.ValueObject.FrequencyName;
                fr.frequencyNameEnglish = helper.ValueObject.FrequencyName_English;

                fr.StartPosition = FormStartPosition.CenterScreen;
                fr.ShowDialog();
            }
        }

        private void DeleteRows()
        {
            if (gridView1.SelectedRowsCount > 0)
            {
                var row = new DataRow[gridView1.SelectedRowsCount];
                var frequencyId = new string[gridView1.SelectedRowsCount];

                for (int i = 0; i < gridView1.SelectedRowsCount; i++)
                {
                    row[i] = gridView1.GetDataRow(gridView1.GetSelectedRows()[i]);
                    frequencyId[i] = row[i][ToolConstants.Entities.Frequency.FieldName.FrequencyID].ToString();
                }


                if (MessageBox.Show(ToolConstants.Message.DeleteQuestion, ToolConstants.Form.MessageBox.Warning,
                                MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    FrequencyHelper helper = new FrequencyHelper(new Frequency());
                    for (int i = 0; i < frequencyId.Length; i++)
                    {
                        helper.ValueObject.FrequencyID = frequencyId[i];
                        if (helper.Delete() > 0)
                        {
                            //write Log
                            log.Table = LogConstant.Table.ToolCutting.Frequency;
                            log.Task = LogConstant.Task.Delete;
                            log.Description = "FrequencyID: " + row[i][ToolConstants.Entities.Frequency.FieldName.FrequencyID].ToString();
                            log.WriteLog();
                            _action = 2;
                        }
                        else
                        {
                            MessageBox.Show(ToolConstants.Message.DeleteFailed, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    BindDatatoGrid();


                }
            }
            else
            {
                MessageBox.Show(ToolConstants.Message.TableIsNull, ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }

        private void EditRow()
        {
            if (gridView1.SelectedRowsCount > 0)
            {
                frm_FrequencyEdit fr = new frm_FrequencyEdit(this);
                var row = new DataRow[gridView1.SelectedRowsCount];
                row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);

                index = gridView1.FocusedRowHandle;
                _action = 1;

                string frequencyId = row[0][ToolConstants.Entities.Frequency.FieldName.FrequencyID].ToString();
                helper.ValueObject.FrequencyID = frequencyId;
                helper.LoadOne();

                fr.frequencyId = frequencyId;
                fr.frequencyName = helper.ValueObject.FrequencyName;
                fr.frequencyNameEnglish = helper.ValueObject.FrequencyName_English;

                fr.StartPosition = FormStartPosition.CenterScreen;
                fr.ShowDialog();
            }
            else
            {
                MessageBox.Show(ToolConstants.Message.TableIsNull, ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }

        private void AddNew()
        {
            Cursor.Current = Cursors.WaitCursor;
            Form fr = new frm_FrequencyAdd(this);
            fr.StartPosition = FormStartPosition.CenterScreen;
            fr.ShowDialog();
            Cursor.Current = Cursors.Default;
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            BindDatatoGrid();
            Cursor.Current = Cursors.Default;
        }

        private void gridControl1_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                DeleteRows();
            }
        }

        protected virtual void InitPopupMenu()
        {
            menu = new DXPopupMenu();
            menu.Items.Add(new DXMenuItem("Export all", new EventHandler(OnItemClickExportAll), null, null, null, null));
            menu.Items.Add(new DXMenuItem("Export selected row(s)", new EventHandler(OnItemClickExportRows), null, null, null, null));

        }
        void OnItemClickExportAll(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 1, true);
        }
        void OnItemClickExportRows(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 0, true);
        }


    }
}
